using System;

namespace Ext {
    public class ScriptSharpAdapter {
        public static void init() {
            // **** renamed classes ****
            Script.Eval("ExtClass = Ext;");
            Script.Eval("ExtString = String;");
            Script.Eval("Ext.UpdaterClass = Ext.Updater;");
            Script.Eval("Ext.ActionClass = Ext.Action;");
            Script.Eval("Ext.SplitBarClass = Ext.SplitBar;");
            Script.Eval("Ext.ToolbarClass = Ext.Toolbar;");
            Script.Eval("Ext.form.ActionClass = Ext.form.Action;");
            Script.Eval("Ext.layout.BorderLayoutClass = Ext.layout.BorderLayout;");

            // **** renamed methods ****
            Script.Eval("Ext.DomQuery.is_ = Ext.DomQuery.is;");
            Script.Eval("Ext.Element.is_ = Ext.Element.is;");
            Script.Eval("Ext.override_ = Ext.override;");
            Script.Eval("Ext.namespace_ = Ext.namespace;");
            Script.Eval("Ext.dd.DragDrop.lock_ = Ext.dd.DragDrop.lock;");
            Script.Eval("Ext.dd.DragDropMgr.lock_ = Ext.dd.DragDropMgr.lock;");
            Script.Eval("Ext.grid.AbstractSelectionModel.lock_ = Ext.grid.AbstractSelectionModel.lock;");

            // **** renamed properties ****
            Script.Eval("Ext.form.Action.params_ = Ext.form.Action.params;");
            Script.Eval("Ext.form.Checkbox.checked_ = Ext.form.Checkbox.checked;");
            Script.Eval("Ext.grid.ColumnModel.fixed_ = Ext.grid.ColumnModel.fixed;");
            Script.Eval("Ext.menu.CheckItem.checked_ = Ext.menu.CheckItem.checked;");

            // changes Script#'s inheritance to use Ext.extend()
            Script.Eval("Type.prototype._createClass=Type.prototype.createClass;Type.prototype.createClass=function(n,b,i){if(b){var ct=b.prototype.ctype;if(ct&&ct.substring(0,3)=='Ext'){delete this.__basePrototypePending;eval(n+' = Ext.extend(b, this.prototype);');return;}}this._createClass(n,b,i);}");

            // **** bug fixes ****

            /* ** Big Fix #1 **
             * Script# overwrites the String.replace() method with a different implementation
             *  that replaces all occurences of the text to search for instead of just the 
             *  first occurrence. This behavior breaks the Ext DomQuery class. Luckily Script#
             *  stores a backup of the original method, so I'll just revert the change back
             *  - sscorlib.debug.js, line #564
             */
            Script.Eval("String.prototype.replace = String.prototype._replace;");
        }
    }
}
